package com.muzi.weshop.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.muzi.weshop.R;
import com.muzi.weshop.common.LoginManager;
import com.muzi.weshop.common.base.BaseActivity;
import com.muzi.weshop.common.contants.ExtraConstants;
import com.muzi.weshop.common.contants.RequestCodeContants;
import com.muzi.weshop.model.AddressModel;
import com.muzi.weshop.model.AdressListModel;
import com.muzi.weshop.view.adapter.AddressAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.IDN;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.rubintry.dialog.ios.IOSMessageDialog;

/**
 * @author 天阳欧巴
 */
public class AddressActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {


    private static final int RESULT_CODE_ADDRESS = 10002;
    private static final int REQUEST_CODE_TO_NEW_ADDRESS = 10005;
    @BindView(R.id.refreshAddress)
    SmartRefreshLayout refreshAddress;
    @BindView(R.id.rvAddress)
    RecyclerView rvAddress;

    private AddressAdapter addressAdapter;

    private List<AdressListModel> addressList;


    private int pageNum;
    private int id;


    @Override
    protected boolean hideStatusBar() {
        return true;
    }

    @Override
    protected int attachedLayout() {
        return R.layout.activity_address;
    }


    @Override
    protected void initViews() {
        pageNum = 1;
        initRecycler();
        refreshAddress.setOnRefreshListener(this);
        refreshAddress.setOnLoadMoreListener(this);
    }

    private void initRecycler() {
        rvAddress.setLayoutManager(new LinearLayoutManager(this));
        rvAddress.setNestedScrollingEnabled(false);
        addressList = new ArrayList<>();
        addressAdapter = new AddressAdapter(R.layout.item_address, addressList);
        rvAddress.setAdapter(addressAdapter);
        addressAdapter.setOnItemClickListener(new AddressAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int id) {

                new IOSMessageDialog.Builder(AddressActivity.this)
                        .setMessage("确定要去结算吗？")
                        .setCancelable(true)
                        .setOnButtonClickListener(new IOSMessageDialog.OnButtonClickListener() {
                            @Override
                            public void onConfirm() {
                                AddressActivity.this.id = id;
                                Intent intent = new Intent();
                                intent.putExtra(ExtraConstants.SELECTED_ADDRESS_ID, id);
                                setResult(RESULT_CODE_ADDRESS, intent);
                                finish();
                            }

                            @Override
                            public void onCancel() {

                            }
                        }).create().show();
            }
        });
    }

    @Override
    protected void requestData() {
        pageNum = 1;
        apiPresenter.getAddressList(LoginManager.getInstance().getPersonalId(), pageNum, 10, RequestCodeContants.GET_ADDRESS_LIST);
    }


    @OnClick({R.id.imgBtnBack, R.id.tvAddAddress})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgBtnBack:
                finish();
                break;
            case R.id.tvAddAddress:
                startActivityForResult(new Intent(this, NewAddressActivity.class), REQUEST_CODE_TO_NEW_ADDRESS);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        pageNum++;
        apiPresenter.getAddressList(LoginManager.getInstance().getPersonalId(), pageNum, 10, RequestCodeContants.GET_MORE_ADDRESS_LIST);
    }


    @Override
    public void onNext(Object o, int requestCode) {
        super.onNext(o, requestCode);
        refreshAddress.finishRefresh();
        refreshAddress.finishLoadMore();
        switch (requestCode) {
            case RequestCodeContants.GET_ADDRESS_LIST:
                AddressModel addressModel = (AddressModel) o;
                if (addressModel == null) {
                    return;
                }

                if (addressModel.getAdressList() == null) {
                    return;
                }
                addressList.clear();
                addressList.addAll(addressModel.getAdressList());
                addressAdapter.setNewData(addressList);
                break;
            case RequestCodeContants.GET_MORE_ADDRESS_LIST:
                AddressModel addressModel1 = (AddressModel) o;
                if (addressModel1 == null) {
                    return;
                }

                if (addressModel1.getAdressList() == null) {
                    return;
                }
                addressList.addAll(addressModel1.getAdressList());
                addressAdapter.setNewData(addressList);
                break;
            default:
                break;
        }
    }


    @Override
    public void onError(Throwable e, int request) {
        super.onError(e, request);
        refreshAddress.finishRefresh();
        refreshAddress.finishLoadMore();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_TO_NEW_ADDRESS:
                requestData();
                break;
            default:
                break;
        }
    }
}
